package com.pkk.spring.cloud.service.gateway.filter.keyresolver;

import java.net.InetSocketAddress;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 请求IP限制解析
 *
 * @author peikunkun
 * @version V1.0
 * @date 2021-01-29 18:03
 **/
public class RequestIpRateLimitKeyResolver implements KeyResolver {


  /**
   * ip限制的bean
   */
  public static final String RequestIpRateLimitKeyResolver_BEANNAME = "requestIpRateLimitKeyResolver";


  /**
   * 值限制解析
   *
   * @return reactor.core.publisher.Mono<java.lang.String>
   * @Param exchange
   * @author peikunkun
   * @date 2021/1/29 0029 下午 6:04
   * @since
   */
  @Override
  public Mono<String> resolve(ServerWebExchange exchange) {
    final InetSocketAddress remoteAddress = exchange.getRequest().getRemoteAddress();
    if (null == remoteAddress) {
      return Mono.just("127.0.0.1");
    }
    return Mono.just(remoteAddress.getAddress().getHostAddress());
  }
}
